<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/11
 * Time: 14:17
 */

namespace App\HttpController\Home;

use App\Utility\Pool\MysqlObject;
use App\Utility\Pool\MysqlPool;
use App\Service\MatchService;

class Match extends Base
{
    /**
     * 已结束的比赛列表
     * @return bool
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\OrderByFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function lists()
    {
        $matchService = new MatchService();
        $start_date = date('Y-m-d 00:00:00');
        $end_date = date("Y-m-d 00:00:00",strtotime("+1 day"));
        $params = [
            'status' => 2,
            'order'  => 'asc',
            'start_date' => $start_date,
            'end_date'   => $end_date
        ];
        $lolMatchs = $matchService->lolMatchLists($params)['lists'];
        foreach( $lolMatchs as $k=>$v ){
            $lolMatchs[$k]['game'] = 'lol';
        }
        $dotaMatchs = $matchService->dotaMatchLists($params)['lists'];
        foreach ($dotaMatchs as $k1=>$v1){
            $dotaMatchs[$k1]['game'] = 'dota';
        }
        $csgoMatchs = $matchService->csgoMatchLists($params)['lists'];
        foreach ($csgoMatchs as $k2=>$v2){
            $csgoMatchs[$k2]['game'] = 'csgo';
        }
        $kogMatchs = $matchService->kogMatchLists($params)['lists'];
        foreach ( $kogMatchs as $k3=>$v3 ){
            $kogMatchs[$k3]['game'] = 'kog';
        }

        $merge_one = array_merge_recursive($lolMatchs,$dotaMatchs);
        $merge_two = array_merge_recursive($merge_one,$csgoMatchs);
        $merge_three = array_merge_recursive($merge_two,$kogMatchs);
        $count = count($merge_three);
        for ( $i=0;$i<$count;$i++ ){ //根据比赛开始时间做排序
            $isSorted = true;
            $sortBorder = $count-1;
            for ( $j=0;$j < $sortBorder;$j++){
                if( $merge_three[$j]['match_time'] > $merge_three[$j+1]['match_time'] ){
                    $merge_three[$j] = $merge_three[$j+1];
                    $merge_three[$j+1] = $merge_three[$j];
                    $isSorted = false;
                    $sortBorder = $j;
                }
            }
            if( $isSorted ){
                break;
            }

        }
        return $this->writeJson(1,$merge_three,'请求成功');
    }

    /**
     * lol比赛列表
     * @return bool
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\OrderByFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \EasySwoole\Mysqli\Exceptions\WhereParserFail
     * @throws \Throwable
     */
    public function lol()
    {
        $params = $this->request()->getQueryParams();
        $page = $params['page'] ?? 1;
        $service = new MatchService();
        $data = $service->lolMatchLists($params);
        return $this->writeJson(1,['currentPage'=>$page,'total'=>$data['total'],'data'=>$data['lists']],'请求成功');
    }

    /**
     * dota比赛列表
     * @return bool
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\OrderByFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function dota()
    {
        $params = $this->request()->getQueryParams();
        $page = $params['page'] ?? 1;
        $service = new MatchService();
        $data = $service->dotaMatchLists($params);
        return $this->writeJson(1,['currentPage'=>$page,'total'=>$data['total'],'data'=>$data['lists']],'请求成功');
    }

    /**
     * 王者荣耀比赛列表
     * @return bool
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\OrderByFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \EasySwoole\Mysqli\Exceptions\WhereParserFail
     * @throws \Throwable
     */
    public function kog()
    {
        $params = $this->request()->getQueryParams();
        $page = $params['page'] ?? 1;
        $service = new MatchService();
        $data = $service->kogMatchLists($params);
        return $this->writeJson(1,['currentPage'=>$page,'total'=>$data['total'],'data'=>$data['lists']],'请求成功');
    }

    /**
     * 比赛详情
     * @return bool
     * @throws \EasySwoole\Component\Pool\Exception\PoolEmpty
     * @throws \EasySwoole\Component\Pool\Exception\PoolException
     * @throws \Throwable
     */
    public function info()
    {
        $params = $this->request()->getQueryParams();
        if( !isset($params['game']) || !$params['game'] ){
            return $this->writeJson(0,null,'缺少game参数');
        }
        if( !isset($params['match_id']) || !$params['match_id'] ){
            return $this->writeJson(0,null,'缺少match_id参数');
        }
        $table = "siam_{$params['game']}matchs";
        $info = MysqlPool::invoke(function (MysqlObject $db)use($table,$params){
            $teamTable = "siam_{$params['game']}teams";

            $info = $db->where('match_id',$params['match_id'])->getOne($table);
            $team_a = $db->where('team_id',$info['team_a_id'])->getOne($teamTable);
            $team_b = $db->where('team_id',$info['team_b_id'])->getOne($teamTable);
            $live_videos = $db->where('game',$params['game']) //直播源
            ->where('match_id',$params['match_id'])->get('siam_live_video',null,'name,url');
            $videos = [
                0=>[
                    'iframe' => [],
                    'web'    => []
                ]
            ];
            if( $live_videos ){
                foreach ( $live_videos as $k=>$v ){
                    if( strstr($v['url'],'iframe') ){
                        if( !$videos[0]['iframe'] ){
                            $videos[0]['iframe'] = [$v];
                        }
                    }else{
                        if(!$videos[0]['web']){
                            $videos[0]['web'] = [$v];
                        }
                    }
                }
            }

            $info['start_time'] = $this->getMsecToMescdate($info['start_time']);
            if( $info['battle_ids'] ){
                $info['battle_ids'] = json_decode($info['battle_ids'],true);
            }else{
                $info['battle_ids'] = [];
            }
            if( isset($info['map_bp']) ){
                $info['map_bp'] = json_decode($info['map_bp'],true);
                if( $info['map_bp'] ){
                    foreach ( $info['map_bp'] as $k1=>$v1 ){
                        if( strstr($v1,'removed') ){
                            $change = ['removed'=>'禁用'];
                            $info['map_bp'][$k1] = strtr($v1,$change);
                        }
                        if( strstr($v1,'picked') ){
                            $change = ['picked'=>'选取'];
                            $info['map_bp'][$k1] = strtr($v1,$change);
                        }
                        if( strstr($v1,'was left over') ){
                            $change = ['was left over'=>'被保留'];
                            $info['map_bp'][$k1] = strtr($v1,$change);
                        }
                    }
                }
            }
            if( isset($info['map_names']) ){
                $info['map_names'] = json_decode($info['map_names'],true);
                if( $info['map_names'] ){
                    foreach ($info['map_names'] as $k=>$v){
                        if( !strstr($v,'de_') ){
                            $info['map_names'][$k] = strtolower('de_'.$v);
                        }
                    }
                }
            }
            if( $params['game'] == 'csgo' ){
                if( $info['bo'] == 1 ){
                    if( $info['team_a_score'] > $info['team_b_score'] ){
                        $info['team_a_score'] = 1;
                        $info['team_b_score'] = 0;
                    }else{
                        $info['team_b_score'] = 1;
                        $info['team_a_score'] = 0;
                    }
                }
                unset($info['battle_list']);
            }
            $info['team_a'] = $team_a;
            $info['team_b'] = $team_b;
            $info['live_video'] = $videos;
            $info['league'] = $db->where('league_id',$info['league_id'])->getOne('siam_'.$params['game'].'league','league_id,logo,name');
            unset($info['league_id']);
            return $info;
        });
        return $this->writeJson(1,$info,'请求成功');
    }

    /**
     * csgo比赛列表
     * @return bool
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\OrderByFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \EasySwoole\Mysqli\Exceptions\WhereParserFail
     * @throws \Throwable
     */
    public function csgo()
    {
        $params = $this->request()->getQueryParams();
        $page = $params['page'] ?? 1;
        $service = new MatchService();
        $data = $service->csgoMatchLists($params);
        return $this->writeJson(1,['currentPage'=>$page,'total'=>$data['total'],'data'=>$data['lists']],'请求成功');
    }

    public function getWeek()
    {
        $date = date('Y-m-d H:i:s',strtotime('-d day'));
    }

}